package com.ms.education.statistics.initApplication;

import com.dangdang.ddframe.job.api.JobType;
import com.ms.education.ServerUtils;
import com.ms.education.constant.OrderConstant;
import com.ms.education.statistics.entity.JobConfigParameters;
import com.ms.education.statistics.handler.ElasticJobHandler;
import com.ms.education.statistics.jobs.DeleteExpireOrderJob;
import com.ms.education.statistics.jobs.StatisticDailyJob;
import com.ms.education.statistics.service.StatisticsDailyService;
import com.ms.education.statistics.service.feign.OrderFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * <p>Project:我的代码-StartedInit
 * <p>Powered by ms on 2023-02-11 22:16:12
 *
 * @author ms
 * @version 1.0
 * @since 1.8
 */
@Component
@Slf4j
public class StartedInit implements  ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private ElasticJobHandler elasticJobHandler;

    @Autowired
    private StatisticsDailyService statisticsDailyService;

    @Autowired
    private OrderFeignService orderFeignService;


    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        JobConfigParameters parameters = new JobConfigParameters();
        parameters.setElasticJob(new StatisticDailyJob(statisticsDailyService));
        parameters.setJobName("StatisticDailyJob");
        parameters.setCron("0 0 1 * * ?");//每日凌晨1点
        parameters.setShardingTotalCount(1);
        parameters.setShardingItemParameters(null);
        parameters.setJobParameter(null);
        parameters.setFailover(true);
        parameters.setMisfire(true);
        parameters.setDescription("日常统计任务");
        parameters.setJobType(JobType.SIMPLE);
        parameters.setJobClass(StatisticDailyJob.class);
        elasticJobHandler.addJob(parameters);
        log.info("项目启动完毕，添加了一个日常统计任务:{}任务",StatisticDailyJob.class);

        parameters.setElasticJob(new DeleteExpireOrderJob(orderFeignService));
        parameters.setJobName("DeleteExpireOrderJob");
        parameters.setCron("0 0 0/2 * * ? *");//每两小时，且在小时的第一秒开始执行  0 0/2 * * * ?（用这个）
        parameters.setDescription("删除超过"+ OrderConstant.expiredTime +"秒未支付的订单");
        parameters.setJobType(JobType.SIMPLE);
        parameters.setJobClass(DeleteExpireOrderJob.class);
        elasticJobHandler.addJob(parameters);
        log.info("项目启动完毕，添加了一个:{}任务",DeleteExpireOrderJob.class);


    }
}
